package cn.epsilon3.utils;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import cn.epsilon3.constant.Base;
import cn.epsilon3.exception.Epsilon3Exception;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import sun.misc.BASE64Encoder;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;

/**
 * <p>
 * web工具类
 * </p>
 *
 * @author epsilon3
 * @since 2023-08-13
 */
public class WebUtils {

    /**
     * 获取请求的request
     *
     * @return
     */
    public static HttpServletRequest request() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attributes.getRequest();
    }

    /**
     * 获取请求的request
     *
     * @return
     */
    public static SaSession SaSession() {
        return StpUtil.getSession();
    }

    /**
     * 设置session
     *
     * @param sessionKey session键
     * @param sessionValue session值
     */
    public static void setSaSessionValue(String sessionKey, Object sessionValue) {
        SaSession saSession = SaSession();
        saSession.set(sessionKey,sessionValue);
    }

    /**
     * 设置session
     *
     * @param sessionKey session键
     * @param sessionValue session值
     */
    public static void setSessionValue(String sessionKey, Object sessionValue) {
        HttpServletRequest request = request();
        request.getSession().setAttribute(sessionKey,sessionValue);
    }

    /**
     * 获取session
     *
     * @param sessionKey session键
     */
    public static Object getSaSessionValue(String sessionKey) {
        SaSession saSession = SaSession();
        return saSession.get(sessionKey);
    }

    /**
     * 获取session
     *
     * @param sessionKey session键
     */
    public static Object getSessionValue(String sessionKey) {
        HttpServletRequest request = request();
        return request.getSession().getAttribute(sessionKey);
    }

    /**
     * 删除session
     *
     * @param sessionKey session键
     */
    public static void removeSaSessionValue(String sessionKey) {
        SaSession saSession = SaSession();
        saSession.delete(sessionKey);
    }

    /**
     * 删除session
     *
     * @param sessionKey session键
     */
    public static void removeSessionValue(String sessionKey) {
        HttpServletRequest request = request();
        request.getSession().removeAttribute(sessionKey);
    }

    /**
     * 生成QR码
     *
     * @param uri
     * @param outputStream
     * @param height
     * @param width
     *
     */
    public static void generateQRCode(String uri, String imageType,OutputStream outputStream, int height, int width) {
        try {
            BitMatrix matrix = new MultiFormatWriter().encode(uri, BarcodeFormat.QR_CODE, width, height);
            MatrixToImageWriter.writeToStream(matrix, imageType, outputStream);
        } catch (Exception e) {
            throw new Epsilon3Exception("generate QR code exception" + e.getMessage());
        }
    }

    /**
     * 生成QR码
     *
     * @param uri
     * @param height
     * @param width
     *
     */
    public static void generateQRCode(String uri, String imageType,HttpServletResponse response, int height, int width) {
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            BitMatrix matrix = new MultiFormatWriter().encode(uri, BarcodeFormat.QR_CODE, width, height);
            MatrixToImageWriter.writeToStream(matrix, imageType, outputStream);
        } catch (Exception e) {
            throw new Epsilon3Exception("generate QR code exception" + e.getMessage());
        }
    }

    /**
     * 生成QR码 base64
     *
     * @param uri
     * @param height
     * @param width
     *
     */
    public static String generateQRCodeBase64(String uri, String imageType,int height, int width) {
        try {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            BitMatrix matrix = new MultiFormatWriter().encode(uri, BarcodeFormat.QR_CODE, width, height);
            MatrixToImageWriter.writeToStream(matrix, imageType, outputStream);
            byte[] data = outputStream.toByteArray();
            BASE64Encoder encoder = new BASE64Encoder();
            return Base.BASE64_IMAGE + encoder.encodeBuffer(data)
                    .trim()
                    .replaceAll("\n", "")
                    .replaceAll("\r", "");
        } catch (Exception e) {
            throw new Epsilon3Exception("generate QR code exception" + e.getMessage());
        }
    }
}
